//
//  NetworkTools.swift
//  QuantumWar
//
//  Created by mysj on 16/7/28.
//  Copyright © 2016年 zeng. All rights reserved.
//

import Foundation
private let upImgUrl = "http://ingressimg.dworld.cn/imgUpload.php"
typealias requestBb = (result: AnyObject?, error: NSError?)->()

func upImg(imgName: String, data: NSData, finished: (isSuccess: Bool) -> ()) {
    
    upload(.POST, upImgUrl, headers: nil, multipartFormData: { (formData) -> Void in
        
        formData.appendBodyPart(data: data, name: "file", fileName: "ssda.png", mimeType: "image/png")
    }, encodingMemoryThreshold: 5 * 1024 * 1024) { (encodingResult) -> Void in
        
        switch encodingResult {
        case .Success(request: let upload, _, _):
            upload.responseJSON(completionHandler: { (response) -> Void in
                
                if response.result.isFailure {
                    print("网络请求失败 \(response.result.error)")
                }
                let flag =  (response.result.value as! NSDictionary).valueForKey("flag") as! NSNumber
                if  flag.intValue == 1 {
                    finished(isSuccess: true)
                } else {
                    print(response.result.value)
                }
            })
        case .Failure(let error):
            print("上传文件编码错误\(error)")
        }
    }
    
}



  